package com.mxprojects.admin
{
	import com.mxprojects.constructors.ButtonLarge;
	import com.mxprojects.constructors.CheckboxConstructor;
	import com.mxprojects.constructors.Dropdown;
	import com.mxprojects.constructors.InputConstructor;
	import com.mxprojects.constructors.SimpleTextField;
	import com.mxprojects.gui.ConfirmDialog;
	import com.mxprojects.gui.DefaultDialog;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.CustomEvent;
	import com.mxprojects.io.DeepLinking;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	import com.mxprojects.views.Admin;
	import com.mxprojects.views.Forums;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class AdminForums extends Admin
	{					
		private static var baseInputWidth:int;
		
		private static var leftNextX:int;
		private static var leftNextY:int;
		private static var categoriesTitle:SimpleTextField;
		private static var categoryNewInput:InputConstructor;
		private static var categoryPositionDropdown:Dropdown;
		private static var categoryCreateButton:ButtonLarge;		
		private static var categoryDeleteLabel:SimpleTextField;
		private static var categoryDeleteDropdown:Dropdown;
		private static var categoryDeleteForumsCheck:CheckboxConstructor;
		private static var categoryDeleteButton:ButtonLarge;
		
		private static var rightNextX:int;
		private static var rightNextY:int;
		private static var forumsTitle:SimpleTextField;
		private static var forumCategoryDropdown:Dropdown;
		private static var forumNewInput:InputConstructor;
		private static var forumDescription:InputConstructor;
		private static var forumPositionDropdown:Dropdown;		
		private static var forumGroupDropdown:Dropdown;		
		private static var forumModeratorDropdown:Dropdown;		
		private static var forumReadModeCheck:CheckboxConstructor;
		private static var forumCreateButton:ButtonLarge;
		private static var forumDeleteLabel:SimpleTextField;
		private static var forumDeleteDropdown:Dropdown;
		private static var forumDeleteThreadsCheck:CheckboxConstructor;
		private static var forumDeleteButton:ButtonLarge;
		
		private static var editForumsButton:ButtonLarge;
			
		public function AdminForums(){}
		
		public static function display():void {			
			//set the text
			setAdminTitle(Language.main(Word.CATEGORIES) + " / " + Language.main(Word.FORUMS));
			
			//setup the content holder
			setupHolders();			
			
			//start the category section
			displayCategoryUI();
			
			//add the new forum stuff
			displayForumUI();
			
			//make sure they are at the proper gap
			setLeftAndRight();
			
			//add a button so enable the edit mode of the categories/forums
//			editForumsButton = new ButtonLarge(Language.main(Word.EDIT_FORUMS), editForums);
//			editForumsButton.y = nextY;
//			holder.addChild(editForumsButton);
		}
		
		private static function displayCategoryUI():void {
			//reset the X, Y and holder
			leftNextX = 0;
			leftNextY = 0;
			
			//this will position the category elements on the holder
			categoriesTitle = new SimpleTextField("<span class=\"adminSubTitle\">" + Language.main(Word.CATEGORIES) + "</span>");
			leftNextY += categoriesTitle.height + Config.TEXT_PADDING;
			leftHolder.addChild(categoriesTitle);
			
			//name input
			categoryNewInput = new InputConstructor(Language.main(Word.NEW_CATEGORY));
			categoryNewInput.y = leftNextY;
			leftNextX += categoryNewInput.inputBox.x;
			leftNextY += categoryNewInput.height + Config.TEXT_PADDING/2;
			leftHolder.addChild(categoryNewInput);
			
			//set the base
			baseInputWidth = categoryNewInput.inputBox.width;
			
			//position dropdown			
			categoryPositionDropdown = buildCategoryDropdown(categoryPositionDropdown, true);
			categoryPositionDropdown.x = leftNextX;
			categoryPositionDropdown.y = leftNextY;
			leftNextY += categoryPositionDropdown.height + Config.TEXT_PADDING;
			leftHolder.addChild(categoryPositionDropdown);
			
			//set the width of the input to match the width of the dropdown
			if(categoryPositionDropdown.width > baseInputWidth){
				categoryNewInput.changeWidth(categoryPositionDropdown.width);
			}
			
			//create the button to add the category
			categoryCreateButton = new ButtonLarge(Language.main(Word.NEW_CATEGORY), addNewCategory);
			categoryCreateButton.x = leftNextX;
			categoryCreateButton.y = leftNextY;
			leftNextY += categoryCreateButton.height + Config.TEXT_PADDING*3;
			leftHolder.addChild(categoryCreateButton);
			
			//create a label for the dropdown
			categoryDeleteLabel = new SimpleTextField("<span class=\"formInputLabel\">" + Language.main(Word.DELETE) + InputConstructor.LABEL_SEPARATOR + "</span>");
			categoryDeleteLabel.x = leftNextX - categoryDeleteLabel.width;
			categoryDeleteLabel.y = leftNextY;
			leftHolder.addChild(categoryDeleteLabel);
			
			//create a dropdown for categories to DELETE
			categoryDeleteDropdown = buildCategoryDropdown(categoryDeleteDropdown);
			categoryDeleteDropdown.x = leftNextX;
			categoryDeleteDropdown.y = Math.round(categoryDeleteLabel.y + (categoryDeleteLabel.height/2 - categoryDeleteDropdown.height/2));
			leftNextY += categoryDeleteDropdown.height + Config.TEXT_PADDING/2;
			leftHolder.addChild(categoryDeleteDropdown);
			
			//add a checkbox incase they want to delete the forums/threads/posts under that category
			categoryDeleteForumsCheck = new CheckboxConstructor(Language.main(Word.DELETE_FORUMS_THREADS_POSTS), "true");
			categoryDeleteForumsCheck.x = leftNextX;
			categoryDeleteForumsCheck.y = leftNextY;
			leftNextY += categoryDeleteDropdown.height + Config.TEXT_PADDING;
			leftHolder.addChild(categoryDeleteForumsCheck);
			
			//the delete button
			categoryDeleteButton = new ButtonLarge("<span class=\"buttonAdminText\">" + Language.main(Word.DELETE_CATEGORY) + "</span>", deleteCategory);
			categoryDeleteButton.x = leftNextX;
			categoryDeleteButton.y = leftNextY;
			leftNextY += categoryDeleteButton.height + Config.TEXT_PADDING;
			leftHolder.addChild(categoryDeleteButton);
		}
		
		private static function displayForumUI():void {
			//reset the X, Y and holder
			rightNextX = 0;
			rightNextY = 0;
			
			forumsTitle = new SimpleTextField("<span class=\"adminSubTitle\">" + Language.main(Word.FORUMS) + "</span>");
			rightNextY += forumsTitle.height + Config.TEXT_PADDING;
			rightHolder.addChild(forumsTitle);
			
			//category of the forums
			forumCategoryDropdown = buildCategoryDropdown(forumCategoryDropdown);
			forumCategoryDropdown.y = rightNextY;
			forumCategoryDropdown.addEventListener(Dropdown.SELECT, forumCategorySelect, false, 0, true);
			rightNextY += forumCategoryDropdown.height + Config.TEXT_PADDING*2;
			rightHolder.addChild(forumCategoryDropdown);
			
			//new forum name
			forumNewInput = new InputConstructor(Language.main(Word.NEW_FORUM));
			forumNewInput.y = rightNextY;
			rightNextX += forumNewInput.inputBox.x;
			rightNextY += forumNewInput.height + Config.TEXT_PADDING/2;
			rightHolder.addChild(forumNewInput);
			
			//the description of the forum
			forumDescription = new InputConstructor(Language.main(Word.DESCRIPTION));
			forumDescription.x = Math.round(rightNextX - forumDescription.inputLabel.width);
			forumDescription.y = rightNextY;
			rightNextY += forumDescription.height + Config.TEXT_PADDING/2;
			rightHolder.addChild(forumDescription);

			//define which group the forum is in
			forumGroupDropdown = buildGroupDropdown(forumGroupDropdown, Language.main(Word.SELECT_VISIBILITY));
			forumGroupDropdown.x = rightNextX;
			forumGroupDropdown.y = rightNextY;
			rightNextY += forumGroupDropdown.height + Config.TEXT_PADDING/2;
			rightHolder.addChild(forumGroupDropdown);
			
			//set the input widths
			if(forumGroupDropdown.width > baseInputWidth){
				forumNewInput.changeWidth(forumGroupDropdown.width);
				forumDescription.changeWidth(forumGroupDropdown.width);
			}
			
			//set the category dropdown position (inputs are made after the dropdown)
			forumCategoryDropdown.x = rightNextX;
			if(forumCategoryDropdown.width < forumGroupDropdown.width){
				forumCategoryDropdown.width = forumGroupDropdown.width;
			}
			
			//select the moderators
			forumModeratorDropdown = buildGroupDropdown(forumModeratorDropdown, Language.main(Word.SELECT_MODERATORS));
			forumModeratorDropdown.x = rightNextX;
			forumModeratorDropdown.y = rightNextY;
			rightNextY += forumModeratorDropdown.height + Config.TEXT_PADDING/2;
			rightHolder.addChild(forumModeratorDropdown);
			
			//add a position dropdown for when the category is selected
			forumPositionDropdown = buildForumDropdown(forumPositionDropdown, true);
			forumPositionDropdown.x = rightNextX;
			forumPositionDropdown.y = rightNextY;
			rightNextY += forumPositionDropdown.height + Config.TEXT_PADDING/2;
			rightHolder.addChild(forumPositionDropdown);				
			
			//read only?
			forumReadModeCheck = new CheckboxConstructor(Language.main(Word.READ_ONLY), "1");
			forumReadModeCheck.x = rightNextX;
			forumReadModeCheck.y = rightNextY;
			rightNextY += forumReadModeCheck.height + Config.TEXT_PADDING;
			rightHolder.addChild(forumReadModeCheck);
			
			//create the forum
			forumCreateButton = new ButtonLarge(Language.main(Word.NEW_FORUM), addNewForum);
			forumCreateButton.x = rightNextX;
			forumCreateButton.y = rightNextY;
			rightNextY += forumCreateButton.height + Config.TEXT_PADDING*3;
			rightHolder.addChild(forumCreateButton);
			
			//create a label for the dropdown
			forumDeleteLabel = new SimpleTextField("<span class=\"formInputLabel\">" + Language.main(Word.DELETE) + InputConstructor.LABEL_SEPARATOR + "</span>");
			forumDeleteLabel.x = rightNextX - forumDeleteLabel.width;
			forumDeleteLabel.y = rightNextY;
			rightHolder.addChild(forumDeleteLabel);
			
			//create a dropdown for forumegories to DELETE
			forumDeleteDropdown = buildForumDropdown(forumDeleteDropdown);
			forumDeleteDropdown.x = rightNextX;
			forumDeleteDropdown.y = Math.round(forumDeleteLabel.y + (forumDeleteLabel.height/2 - forumDeleteDropdown.height/2));
			rightNextY += forumDeleteDropdown.height + Config.TEXT_PADDING/2;
			rightHolder.addChild(forumDeleteDropdown);
			
			//add a checkbox incase they want to delete the forums/threads/posts under that forum
			forumDeleteThreadsCheck = new CheckboxConstructor(Language.main(Word.DELETE_THREADS_POSTS), "true");
			forumDeleteThreadsCheck.x = rightNextX;
			forumDeleteThreadsCheck.y = rightNextY;
			rightNextY += forumDeleteDropdown.height + Config.TEXT_PADDING;
			rightHolder.addChild(forumDeleteThreadsCheck);
			
			//the delete button
			forumDeleteButton = new ButtonLarge("<span class=\"buttonAdminText\">" + Language.main(Word.DELETE_FORUM) + "</span>", deleteForum);
			forumDeleteButton.x = rightNextX;
			forumDeleteButton.y = rightNextY;
			rightNextY += forumDeleteButton.height + Config.TEXT_PADDING;
			rightHolder.addChild(forumDeleteButton);
		}
		
		private static function addNewCategory(event:MouseEvent):void {
			//make sure they have something set
			if(categoryNewInput.getValue() == "" || categoryPositionDropdown.value == Dropdown.NULL_VALUE){
				DefaultDialog.show(Language.main(Word.ERROR_REQURED_FIELDS));
				return;
			}
			
			//prepare the data to head to the server
			dataToSend = new Object();
			dataToSend.action = AdminActions.ADD_CATEGORY;
			dataToSend.categoryName = categoryNewInput.inputBox.text;
			dataToSend.categoryPosition = categoryPositionDropdown.value;
			
			var adminHandleData:AdminHandleData = new AdminHandleData(dataToSend, newCategorySuccess, categoryCreateButton, ADMIN_SCRIPT);
		}
		
		private static function newCategorySuccess(data:Object):void {
			//the category was created
			DefaultDialog.show(Language.main(Word.MESSAGE_NEW_CATEGORY), true, Word.SUCCESS);
			
			//refresh the category dropdowns and clear the name
			categoryPositionDropdown.addItemAt(Language.main(Word.AFTER) + " " + categoryNewInput.getValue(), int(categoryPositionDropdown.value) + 1, int(categoryPositionDropdown.value) + 3);
			categoryDeleteDropdown.addItemAt(categoryNewInput.getValue(), data.newCategoryID, int(categoryPositionDropdown.value) + 2);
			forumCategoryDropdown.addItemAt(categoryNewInput.getValue(), data.newCategoryID, int(categoryPositionDropdown.value) + 2);
			
			categoryNewInput.inputBox.text = "";
			if(categoryPositionDropdown.width > baseInputWidth){
				categoryNewInput.changeWidth(categoryPositionDropdown.width);
			}
			
			//in case things are getting big/small
			setLeftAndRight();
		}
		
		private static function deleteCategory(event:MouseEvent):void {
			//anything to delete?
			if(categoryDeleteDropdown.value == Dropdown.NULL_VALUE){
				return;
			}
			
			//make sure this is something they want to do
			ConfirmDialog.confirm(Language.main(Word.DELETE_CONFIRM), Language.main(Word.DELETE, true), deleteCategoryConfirm);
		}
		
		private static function deleteCategoryConfirm():void {
			//this confirms the delete and sends it off to the server
			dataToSend = new Object();
			dataToSend.action = AdminActions.DELETE_CATEGORY;
			dataToSend.categoryID = categoryDeleteDropdown.value;
			dataToSend.deleteForums = categoryDeleteForumsCheck.getValueIfChecked();
			
			var adminHandleData:AdminHandleData = new AdminHandleData(dataToSend, deleteCategorySuccess, categoryDeleteButton,  ADMIN_SCRIPT);
		}
		
		private static function deleteCategorySuccess(data:Object):void {
			//it's nuked. Update the dropdowns and let the user know what happened
			DefaultDialog.show(Language.main(Word.MESSAGE_CATEGORY_DELETED), true, Word.SUCCESS);
			
			//force an overwrite of the current server data
			dataFromServer = data;
			
			//rebuild the dropdowns
			categoryPositionDropdown = buildCategoryDropdown(categoryPositionDropdown, true);
			categoryDeleteDropdown = buildCategoryDropdown(categoryDeleteDropdown);
			forumCategoryDropdown = buildCategoryDropdown(forumCategoryDropdown);
			forumPositionDropdown = buildForumDropdown(forumPositionDropdown, true);
			
			//change the width of the new category if the position changed
			if(categoryPositionDropdown.width > baseInputWidth){
				categoryNewInput.changeWidth(categoryPositionDropdown.width);
			}			
			
			//put the checkbox back to unchecked
			categoryDeleteForumsCheck.setCheckState(false);
			
			//in case things are getting big/small
			setLeftAndRight();
		}
		
		private static function forumCategorySelect(event:CustomEvent):void {
			//this will light up any disabled forum dropdowns that require the need of a categoryID
			var categoryID:int = event.customParameter[1];
			
			forumPositionDropdown = buildForumDropdown(forumPositionDropdown, true, categoryID);
			forumDeleteDropdown = buildForumDropdown(forumDeleteDropdown, false, categoryID);
		}
		
		
		private static function addNewForum(event:MouseEvent):void {
			//make sure everything is set
			if(forumNewInput.getValue() == "" || forumDescription.getValue() == ""){
				DefaultDialog.show(Language.main(Word.ERROR_REQURED_FIELDS));
				return;
			}
						
			if(forumCategoryDropdown.value == Dropdown.NULL_VALUE || 
			   forumGroupDropdown.value == Dropdown.NULL_VALUE || 
			   forumModeratorDropdown.value == Dropdown.NULL_VALUE || 
			   forumPositionDropdown.value == Dropdown.NULL_VALUE){
					DefaultDialog.show(Language.main(Word.ERROR_EMPTY_DROPDOWN));
					return;
			}
			
			//disable the button
			forumCreateButton.enabled = false;
			
			//prepare the data to head to the server
			dataToSend = new Object();
			dataToSend.action = AdminActions.ADD_FORUM;
			dataToSend.categoryID = forumCategoryDropdown.value;
			dataToSend.groupID = forumGroupDropdown.value;
			dataToSend.moderatorGroupID = forumModeratorDropdown.value;
			dataToSend.forumName = forumNewInput.getValue();
			dataToSend.forumDescription = forumDescription.getValue();
			dataToSend.forumReadMode = forumReadModeCheck.getValueIfChecked();
			dataToSend.forumPosition = forumPositionDropdown.value;
			
			var adminHandleData:AdminHandleData = new AdminHandleData(dataToSend, newForumSuccess, forumCreateButton, ADMIN_SCRIPT);
		}
		
		private static function newForumSuccess(data:Object):void {
			//the forum was created
			DefaultDialog.show(Language.main(Word.MESSAGE_NEW_FORUM), true, Word.SUCCESS);
			
			//refresh the forum dropdowns and clear the name
			forumPositionDropdown.addItemAt(Language.main(Word.AFTER) + " " + forumNewInput.getValue(), int(forumPositionDropdown.value) + 1, int(forumPositionDropdown.value) + 3);
			
			forumNewInput.inputBox.text = "";
			forumDescription.inputBox.text = "";
			
			//in case things are getting big/small
			setLeftAndRight();
		}
		
		private static function deleteForum(event:MouseEvent):void {
			//anything to delete?
			if(forumDeleteDropdown.value == Dropdown.NULL_VALUE){
				return;
			}
			
			//make sure this is something they want to do
			ConfirmDialog.confirm(Language.main(Word.DELETE_CONFIRM), Language.main(Word.DELETE, true), deleteForumConfirm);
		}
		
		private static function deleteForumConfirm():void {
			//this confirms the delete and sends it off to the server
			dataToSend = new Object();
			dataToSend.action = AdminActions.DELETE_FORUM;
			dataToSend.categoryID = forumCategoryDropdown.value;
			dataToSend.forumID = forumDeleteDropdown.value;
			dataToSend.deleteThreads = forumDeleteThreadsCheck.getValueIfChecked();
			
			var adminHandleData:AdminHandleData = new AdminHandleData(dataToSend, deleteForumSuccess, forumDeleteButton,  ADMIN_SCRIPT);
		}
		
		private static function deleteForumSuccess(data:Object):void {
			//it's nuked. Update the dropdowns and let the user know what happened
			DefaultDialog.show(Language.main(Word.MESSAGE_FORUM_DELETED), true, Word.SUCCESS);
			
			//force an overwrite of the current server data
			dataFromServer = data;
			
			//rebuild the dropdowns
			forumCategoryDropdown = buildCategoryDropdown(forumCategoryDropdown);
			forumPositionDropdown = buildForumDropdown(forumPositionDropdown, true);		
			forumDeleteDropdown = buildForumDropdown(forumDeleteDropdown);		
			
			//put the checkbox back to unchecked
			forumDeleteThreadsCheck.setCheckState(false);
			
			//in case things are getting big/small
			setLeftAndRight();
		}
		
		private static function processData(event:CustomEvent):void {
			var forumData:Object = event.getServerData();
			
			//see what action we are dealing with
			if(forumData.output == "success"){
				switch(forumData.action){
					case AdminActions.ADD_CATEGORY:
						//let them know it went ok
						DefaultDialog.show(Language.main(Word.MESSAGE_NEW_CATEGORY), false, Word.SUCCESS);
												
						//refresh the panel so the new category shows up in the dropdown
						changeLocation(Word.FORUMS);
					break;
					
					case AdminActions.ADD_FORUM:
						//refresh the main forums list if they are there
						if(MXP3.currentLocation == Word.FORUMS){
							Forums.load();
						}
						
						//enable the button and clear the text blocks
						forumCreateButton.enabled = true;
						forumNewInput.inputBox.text = "";
						forumDescription.inputBox.text = "";
						forumReadModeCheck.setCheckState(false);
					break;
				}
			}
		}
		
		private static function editForums(event:MouseEvent):void {
			//make sure we are on the forums area
			if(MXP3.currentLocation != Word.FORUMS){
				DeepLinking.setLink(Word.FORUMS, DeepLinking.createLinkArray());
				Forums.loadForums.listenForData(loadEditForums);
			}else{
				loadEditForums();
			}
		}
		
		private static function loadEditForums(event:CustomEvent = null):void {
			//roll up the admin so we can drag stuff around
			popup.rollUp();
			
			//since this is a lot of stuff, it's in it's own class
			AdminEditForums.init();
		}
		
		private static function buildCategoryDropdown(categoryDropdown:Dropdown, isPositions:Boolean = false):Dropdown {			
			var firstElement:String = Language.main(Word.SELECT_CATEGORY);
			var catDrop:Dropdown = categoryDropdown;
			if(catDrop == null){
				catDrop = new Dropdown();
			}
			
			//make sure it's empty
			catDrop.removeAll();
			
			//if we are doing positions the first element is different
			if(isPositions){
				firstElement = Language.main(Word.SELECT_POSITION);
			}
			
			//put the first value in there
			catDrop.addItem(firstElement, Dropdown.NULL_VALUE, false);
			
			//setup a text var that will prepend the value
			var preText:String = "";
			
			if(isPositions){
				//this is so we can say "After (categoryName)"
				preText = Language.main(Word.AFTER) + " ";
				
				//slam in a quick "First" element for the dropdown
				catDrop.addItem(Language.main(Word.FIRST), -1, false);
			}
			
			//create the dropdown elements 
			for(var i:int = 0; i < dataFromServer.categoryTotal; i++){
				var dropValue:int = dataFromServer["category" + i + "ID"];
				if(isPositions){
					//if it's positions, then we care about position and not ID
					dropValue = dataFromServer["category" + i + "DisplayOrder"];
				}
				catDrop.addItem(preText + dataFromServer["category" + i + "Name"], dropValue, false);
			}
			
			//update the dropdown
			catDrop.update();
			
			return catDrop;
		}
		
		private static function buildForumDropdown(forumDropdown:Dropdown, isPositions:Boolean = false, categoryID:int = -1):Dropdown {
			//this will create an empty, disabled forum dropdown.
			var firstElement:String = Language.main(Word.SELECT_FORUM);
			var forumDrop:Dropdown = forumDropdown;
			if(forumDrop == null){
				forumDrop = new Dropdown();
			}
			
			//remove everything
			forumDrop.removeAll();
			
			//if we are doing positions the first element is different
			if(isPositions){
				firstElement = Language.main(Word.SELECT_POSITION);
			}
			
			//put the first value in there
			forumDrop.addItem(firstElement, Dropdown.NULL_VALUE, true);

			//disable it until a category is selected
			forumDrop.enabled = false;
			
			
			//if we have a category
			if(categoryID > -1){
				//setup a text var that will prepend the value
				var preText:String = "";
				
				if(isPositions){
					//this is so we can say "After (categoryName)"
					preText = Language.main(Word.AFTER) + " ";
					
					//slam in a quick "First" element for the dropdown
					forumDrop.addItem(Language.main(Word.FIRST), -1, false);
				}
				
				//create the dropdown elements 
				var totalForums:int = dataFromServer["category" + categoryID + "TotalForums"];
				for(var i:int = 0; i < totalForums; i++){
					var dropValue:int = dataFromServer["category" + categoryID + "Forum" + i + "ID"];
					if(isPositions){
						//if it's positions, then we care about position and not ID
						dropValue = dataFromServer["category" + categoryID + "Forum" + i + "DisplayOrder"];
					}
					forumDrop.addItem(preText + dataFromServer["category" + categoryID + "Forum" + i + "Name"], dropValue, false);
				}
				
				//update the dropdown
				forumDrop.update();
				
				//enable it
				forumDrop.enabled = true;
			}
			
			return forumDrop;
			
		}
	}
}